/**
 * 
 */
package example.topic.services.impl;

import java.util.List;

import example.business.api.topic.Answer;
import example.business.api.topic.Question;
import example.business.api.topic.Tag;
import example.topic.dal.object.TopicAnswerDO;
import example.topic.dal.object.TopicQuestionDO;
import example.topic.dal.object.TopicTagDO;

/**
 * @author shipeng
 */
public class BeanUtil {

  public static TopicQuestionDO toTopicQuestionDO(Question question) {
    TopicQuestionDO topicQuestionDO = new TopicQuestionDO();
    topicQuestionDO.setId(question.getId());
    topicQuestionDO.setSysInsertTime(question.getSysInsertTime());
    topicQuestionDO.setQuestionContent(question.getQuestionContent());
    topicQuestionDO.setRemark(question.getRemark());
    topicQuestionDO.setClickcount(question.getClickcount());
    topicQuestionDO.setCommentcount(question.getCommentcount());
    topicQuestionDO.setAnswercount(question.getAnswercount());
    if (question.getTags() != null && question.getTags().size() > 0) {
      for (Tag tag : question.getTags()) {
        topicQuestionDO.addTagid(tag.getId());
      }
    }
    topicQuestionDO.setDeleted(question.isDeleted());
    topicQuestionDO.setImageFiles(question.getImageFiles());
    return topicQuestionDO;
  }

  public static Question toQuestion(TopicQuestionDO topicQuestionDO, List<Tag> tags) {
    Question question = new Question();
    question.setId(topicQuestionDO.getId());
    question.setSysInsertTime(topicQuestionDO.getSysInsertTime());
    question.setQuestionContent(topicQuestionDO.getQuestionContent());
    question.setRemark(topicQuestionDO.getRemark());
    question.setClickcount(topicQuestionDO.getClickcount());
    question.setCommentcount(topicQuestionDO.getCommentcount());
    question.setAnswercount(topicQuestionDO.getAnswercount());
    question.setTags(tags);
    question.setDeleted(topicQuestionDO.isDeleted());
    question.setImageFiles(topicQuestionDO.getImageFiles());
    return question;
  }

  public static Tag toTag(TopicTagDO topicTagDO) {

    Tag tag = new Tag();
    tag.setId(topicTagDO.getId());
    tag.setTagname(topicTagDO.getTagname());

    return tag;

  }

  public static TopicTagDO toTopicTag(Tag tag) {
    TopicTagDO topicTagDO = new TopicTagDO();
    topicTagDO.setId(tag.getId());
    topicTagDO.setTagname(tag.getTagname());
    return topicTagDO;
  }

  public static Answer toAnswer(TopicAnswerDO topicAnswerDO, Question question) {
    Answer answer = new Answer();
    answer.setId(topicAnswerDO.getId());
    answer.setAnswerContent(topicAnswerDO.getAnswerContent());
    answer.setCommentcount(topicAnswerDO.getCommentcount());
    answer.setQuestion(question);
    answer.setSysInsertTime(topicAnswerDO.getSysInsertTime());
    return answer;
  }

  public static TopicAnswerDO toTopicAnswer(Answer answer) {
    TopicAnswerDO topicAnswerDO = new TopicAnswerDO();
    topicAnswerDO.setAnswerContent(answer.getAnswerContent());
    topicAnswerDO.setCommentcount(answer.getCommentcount());
    topicAnswerDO.setId(answer.getId());
    topicAnswerDO.setQuestionID(answer.getQuestion().getId());
    topicAnswerDO.setSysInsertTime(answer.getSysInsertTime());
    return topicAnswerDO;
  }

}
